<template>
  <div style="display: flex; flex-direction: column; align-items: center">
    <Danmaku
      ref="danma"
      :paused="paused"
      style="
        width: 600px;
        height: 450px;
        background-color: black;
        resize: both;
        overflow: hidden;
      ">
      <template #="danmakuItem">
        <div
          @mouseenter="toggleItemPaused(danmakuItem)"
          @mouseleave="toggleItemPaused(danmakuItem)"
          style="cursor: pointer">
          {{ danmakuItem.text }}
        </div>
      </template>
    </Danmaku>
    <div class="danma-controls">
      <select v-model="danmaItem.type">
        <option value="move">move</option>
        <option value="top">top</option>
        <option value="bottom">bottom</option>
      </select>
      <input type="color" v-model="danmaItem.color" />
      <input v-model="danmaItem.text" />
      <button @click="draw">发送</button>
      <button @click="togglePaused">{{ paused ? "播放" : "暂停" }}</button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import Danmaku from "@/index";
import type { DanmakuItem } from "@/index";

const danma = ref<InstanceType<typeof Danmaku>>();

const paused = ref(false);
const togglePaused = () => {
  paused.value = !paused.value;
};
const toggleItemPaused = (da: DanmakuItem) => {
  if (!da.$el) return;
  if (da.$el.style.animationPlayState === "paused") {
    da.$el.style.animationPlayState = "running";
  } else {
    da.$el.style.animationPlayState = "paused";
  }
};

const danmaItem = ref<DanmakuItem>({
  text: "",
  type: "move",
  color: "#ffffff",
});
const draw = () => {
  const item = { ...danmaItem.value };
  danma.value?.draw([item]);
};
</script>

<style scoped>
.danma-controls {
  margin-top: 10px;
  display: flex;
  gap: 10px;
  align-items: center;
}

.danma-controls > * {
  display: block;
  height: 24px;
  box-sizing: border-box;
  border-radius: 4px;
  border: 1px solid grey;
}

button {
  background-color: #0078d4;
  color: white;
}
</style>
